package com.rizzo.android.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.rizzo.android.R;
import com.rizzo.android.ui.MainPagerActivity;

import java.util.ArrayList;
import java.util.List;

public class NavigationListFragment extends ListFragment {

    public static final String CURRENT_POSITION_STATE = "currentPositionState";

    private static List<NavigationItem> navigationItems;
    private int currentPosition = 0;

    static {
        navigationItems = new ArrayList<NavigationItem>();
        navigationItems.add(new NavigationItem(R.string.navigation_info, R.drawable.ic_action_info, 0));
        navigationItems.add(new NavigationItem(R.string.navigation_performance, R.drawable.ic_action_performance, 1));
        navigationItems.add(new NavigationItem(R.string.navigation_trend, R.drawable.ic_action_trend, 2));
        navigationItems.add(new NavigationItem(R.string.navigation_quotas, R.drawable.ic_action_quotas, 3));
        navigationItems.add(new NavigationItem(R.string.navigation_errors, R.drawable.ic_action_errors, 4));
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putInt(CURRENT_POSITION_STATE, this.currentPosition);
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            currentPosition = savedInstanceState.getInt(CURRENT_POSITION_STATE);
        }
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.navigation_list, null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ListView listView = getListView();
        final MainPagerActivity mainPagerActivity = (MainPagerActivity) getActivity();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                Log.d(this.getClass().getCanonicalName(), "Activity: " + String.valueOf(mainPagerActivity));
                mainPagerActivity.changePage(view, position);
            }
        });
        Log.d(this.getClass().getCanonicalName(), "onViewCreated");
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        NavigationAdapter adapter = new NavigationAdapter(getActivity());
        adapter.addAll(navigationItems);
        setListAdapter(adapter);
    }

    public static class NavigationItem {
        public int title;
        public int icon;
        public int position;

        public NavigationItem(int tag, int icon, int position) {
            this.title = tag;
            this.icon = icon;
            this.position = position;
        }
    }

    public class NavigationAdapter extends ArrayAdapter<NavigationItem> {

        public NavigationAdapter(Context context) {
            super(context, 0);
        }

        public View getView(final int pos, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.navigation_list_row, null);
            }
            currentPosition = pos;
            ImageView icon = (ImageView) convertView.findViewById(R.id.row_icon);
            icon.setImageResource(getItem(pos).icon);
            TextView title = (TextView) convertView.findViewById(R.id.row_title);
            title.setTextColor(getResources().getColor(R.color.navigation_list_text));
            title.setText(getItem(pos).title);
            return convertView;
        }

    }

    public NavigationItem getCurrentNavigationItem() {
        return navigationItems.get(currentPosition);
    }

    public void setCurrentPosition(int position) {
        this.currentPosition = position;
    }
}
